package com.jcli._java_swing_入门教程pdf.ch17;

import javax.swing.*;
import java.awt.*;

/**
 * @author li
 * @version 1.0
 * @belongsProject novel_20230923
 * @belongsPackage com.jcli._java_swing_入门教程pdf.ch17
 * @className GridBagLayoutDemo
 * @description TODO  网格包布局管理器
 * @createTime 2024-01-15  23:43
 */
public class GridBagLayoutDemo {
    /**
     * 向JFrame中添加JButton按钮
     *
     * @param title         按钮标题
     * @param frame         框架
     * @param gridBagLayout 网格袋布局
     * @param constraints   网格袋约束
     */
    public static void makeButton(String title, JFrame frame, GridBagLayout gridBagLayout, GridBagConstraints constraints) {
        JButton button = new JButton(title);
        gridBagLayout.setConstraints(button, constraints);
        frame.add(button);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("拨号盘");
        //    创建GridBagLayout布局管理器
        GridBagLayout gridBagLayout = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        frame.setLayout(gridBagLayout);
        //组件填充显示区域
        constraints.fill = GridBagConstraints.BOTH;
        //    恢复默认值
        constraints.weightx = 0.0;
        //    结束行
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        JTextField tf = new JTextField("13512345345");
        gridBagLayout.setConstraints(tf, constraints);
        frame.add(tf);
        constraints.weightx = 0.5;
        constraints.weighty = 0.2;
        constraints.gridwidth = 1;
        String num = "7,8,9,4,5,6,1,2,3,返回,拨号";
        //    调用方法，添加按钮组件
        makeButton("7", frame, gridBagLayout, constraints);
        makeButton("8", frame, gridBagLayout, constraints);
        //结束行
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        makeButton("9", frame, gridBagLayout, constraints);
        //重新设置gridwidth的值
        constraints.gridwidth = 1;
        makeButton("4", frame, gridBagLayout, constraints);
        makeButton("5", frame, gridBagLayout, constraints);
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        makeButton("6", frame, gridBagLayout, constraints);
        constraints.gridwidth = 1;
        makeButton("1", frame, gridBagLayout, constraints);
        makeButton("2", frame, gridBagLayout, constraints);
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        makeButton("3", frame, gridBagLayout, constraints);
        constraints.gridwidth = 1;
        makeButton("返回", frame, gridBagLayout, constraints);
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        makeButton("拨号", frame, gridBagLayout, constraints);
        constraints.gridwidth = 1;
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
